package com.zwz.crawler.productdata;

import com.zwz.constant.Constant;
import com.zwz.parse.JsonParserUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;

@Slf4j
public class TungTm {

    public static void startTm( ) {
        // 请求参数
        String boundary = "----WebKitFormBoundaryEjgghMBMkO7Wg3BU";
        String url = "https://sales.tungee.com/api/cooperative/customer/search";

        // 构建 multipart 请求体
        String requestBody = buildMultipartBody(boundary);

        try {
            Connection.Response response = Jsoup.connect(url)
                    .method(Connection.Method.POST)
                    .header("Content-Type", "multipart/form-data; boundary=" + boundary)
                    // 设置完整请求头
                    .header("authority", "sales.tungee.com")
                    .header("accept", "*/*")
                    .header("accept-encoding", "gzip, deflate, br, zstd")
                    .header("accept-language", "zh-CN,zh;q=0.9")
                    .header("cache-control", "no-cache")
                    .header("cookie", Constant.cookies)
                    .header("origin", "https://sales.tungee.com")
                    .header("pragma", "no-cache")
                    .header("priority", "u=1, i")
                    .header("referer", "https://sales.tungee.com/enterprise-details/577de19eadf21dc4/enterprise-information/bidding")
                    .header("sec-ch-ua", "\"Not;A=Brand\";v=\"24\", \"Chromium\";v=\"128\"")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "\"Windows\"")
                    .header("sec-fetch-dest", "empty")
                    .header("sec-fetch-mode", "cors")
                    .header("sec-fetch-site", "same-origin")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36")
                    .header("x-tonxis-pid", "67864ea75f69e05d3b544ca7")
                    // 关键配置
                    .ignoreContentType(true)
                    .maxBodySize(0)
                    .timeout(30000)
                    .requestBody(requestBody)
                    .execute();

           log.info("HTTP状态码: " + response.statusCode());
           //log.info("响应内容:\n" + response.body());
            handleErrors(response);


            //解析
            JsonParserUtil.parseTm(response.body());


        } catch (IOException e) {
            System.err.println("请求异常: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static String buildMultipartBody(String boundary) {
        StringBuilder body = new StringBuilder();

        // 添加 nameId 参数
        body.append("--").append(boundary).append("\r\n")
                .append("Content-Disposition: form-data; name=\"nameId\"\r\n\r\n")
                .append("577de19eadf21dc4\r\n");

        // 添加 begin 参数
        body.append("--").append(boundary).append("\r\n")
                .append("Content-Disposition: form-data; name=\"begin\"\r\n\r\n")
                .append("0\r\n");

        // 添加 end 参数
        body.append("--").append(boundary).append("\r\n")
                .append("Content-Disposition: form-data; name=\"end\"\r\n\r\n")
                .append("5\r\n");

        // 添加 filter 参数
        body.append("--").append(boundary).append("\r\n")
                .append("Content-Disposition: form-data; name=\"filter\"\r\n\r\n")
                .append("{}\r\n");

        // 结束边界
        body.append("--").append(boundary).append("--\r\n");


        return body.toString();
    }

    private static void handleErrors(Connection.Response response) {
        switch (response.statusCode()) {
            case 400:
                System.err.println("[参数错误] 请检查：");
                System.err.println("1. filter参数必须为有效JSON（当前值：{}）");
                System.err.println("2. begin/end分页值有效性");
                break;
            case 401:
                System.err.println("[认证过期] 需要：");
                System.err.println("1. 更新Cookie中的accountCenterSessionId");
                System.err.println("2. 检查请求签名（可能需要x-tonxis-signature头）");
                break;
            case 413:
                System.err.println("[请求过大] 建议：");
                System.err.println("1. 减少分页区间（end - begin <= 100）");
                break;
            case 500:
                System.err.println("[服务错误] 可能原因：");
                System.err.println("1. 请求体格式错误（检查boundary格式）");
                System.err.println("2. 服务器处理异常（稍后重试）");
                break;
        }
    }


}
